package com.tezign.intelligence.mini.dam.aifamilyconsultant.controller;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.tezign.intelligence.mini.dam.aifamilyconsultant.bean.ResponseBean;
import com.tezign.intelligence.mini.dam.aifamilyconsultant.constant.Constant;
import com.tezign.intelligence.mini.dam.aifamilyconsultant.entity.*;
import com.tezign.intelligence.mini.dam.aifamilyconsultant.service.ILargeModelService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.RequiredArgsConstructor;
import lombok.extern.slf4j.Slf4j;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.Objects;


/**
 * <p>
 * 模型信息 前端控制器
 * </p>
 *
 * @author six
 * @since 2024-11-16
 */
@Api(tags = "模型管理")
@Slf4j
@CrossOrigin("*")
@RestController
@RequiredArgsConstructor
@RequestMapping(Constant.APPLICATION_NAME + "/largeModel")
public class LargeModelController {

	private final ILargeModelService largeModelService;

	// 创建模型
	@PostMapping("/create")
	@ApiOperation(value = "创建模型")
//	@RequestParam("largeModelName") String largeModelName,
//	@RequestParam("key") String key
	public ResponseBean<Boolean> create(@RequestBody LargeModel largeModel) {
		LargeModel dbLargeModel = new LargeModel();
		dbLargeModel.setName(largeModel.getName());
		dbLargeModel.setModel(largeModel.getModel());
		dbLargeModel.setEnable("禁用");
		dbLargeModel.setScope("禁用");
		dbLargeModel.setCreateTime(new Date());
		dbLargeModel.setUpdateTime(new Date());
		return new ResponseBean<Boolean>().success(largeModelService.save(dbLargeModel));
	}

	// 修改模型
	@PostMapping("/update")
	@ApiOperation(value = "修改模型")
//	@RequestParam("largeModelName") String largeModelName,
//	@RequestParam("key") String key
	public ResponseBean<Boolean> update(@RequestBody LargeModel largeModel) {
		LargeModel dbLargeModel = largeModelService.getById(largeModel.getId());
		dbLargeModel.setModel(largeModel.getModel());
		dbLargeModel.setName(largeModel.getName());
		return new ResponseBean<Boolean>().success(largeModelService.updateById(dbLargeModel));
	}

	// 切换聊天模型
	@PostMapping("/switchChatLargeModel/{largeModelId}")
	@ApiOperation(value = "切换聊天模型")
	public ResponseBean<Boolean> switchChatLargeModel(@PathVariable("largeModelId") Long largeModelId) {
		LambdaQueryWrapper<LargeModel> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(LargeModel::getEnable, "启用");
		LargeModel dbLargeModel = largeModelService.getOne(wrapper);
		if (Objects.nonNull(dbLargeModel)){
			dbLargeModel.setEnable("禁用");
			largeModelService.updateById(dbLargeModel);
		}
		return new ResponseBean<Boolean>().success(updateChatLargeModel(largeModelId));
	}

	// 切换报告模型
	@PostMapping("/switchReportLargeModel/{largeModelId}")
	@ApiOperation(value = "切换报告模型")
	public ResponseBean<Boolean> switchReportLargeModel(@PathVariable("largeModelId") Long largeModelId) {
		LambdaQueryWrapper<LargeModel> wrapper = new LambdaQueryWrapper<>();
		wrapper.eq(LargeModel::getScope, "启用");
		LargeModel dbLargeModel = largeModelService.getOne(wrapper);
		if (Objects.nonNull(dbLargeModel)){
			dbLargeModel.setScope("禁用");
			largeModelService.updateById(dbLargeModel);
		}
		return new ResponseBean<Boolean>().success(updateReportLargeModel(largeModelId));
	}

	// 删除模型
	@DeleteMapping("/deleteLargeModel/{largeModelId}")
	@ApiOperation(value = "删除模型")
	public ResponseBean<Boolean> deleteLargeModel(@PathVariable("largeModelId") Long largeModelId) {
		return new ResponseBean<Boolean>().success(largeModelService.removeById(largeModelId));
	}

	private Boolean updateChatLargeModel(Long largeModelId){
		LargeModel largeModel = largeModelService.getById(largeModelId);
		largeModel.setEnable("启用");
		return largeModelService.updateById(largeModel);
	}

	private Boolean updateReportLargeModel(Long largeModelId){
		LargeModel largeModel = largeModelService.getById(largeModelId);
		largeModel.setScope("启用");
		return largeModelService.updateById(largeModel);
	}

	@PostMapping("/queryLargeModelList")
	@ApiOperation(value = "获取模型列表")
	public ResponseBean<Page<LargeModel>> queryFamilyList(@RequestBody LargeModel largeModel) {
		Page<LargeModel> pageInfo = new Page<>(largeModel.getCurrent(), largeModel.getPageSize());
		Page<LargeModel> largeModelList = largeModelService.page(pageInfo,
				new LambdaQueryWrapper<LargeModel>()
						.like(Objects.nonNull(largeModel.getName()), LargeModel::getName, largeModel.getName())
						.orderByAsc(LargeModel::getId)
		);
		return new ResponseBean<Page<LargeModel>>().success(largeModelList);
	}
}
